package com.foreverwin.jzymes.report.sfc.service.impl;

import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.production.gr.domain.GoodsReceiptLog;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.enums.ProcedureEnum;
import com.foreverwin.jzymes.production.sfc.mapper.SFCAdviceMapper;
import com.foreverwin.jzymes.production.sfc.service.ISFCService;
import com.foreverwin.jzymes.report.sfc.mapper.SFCReportMapper;
import com.foreverwin.jzymes.report.sfc.service.ISfcReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SfcReportServiceImpl implements ISfcReportService {
    @Autowired
    ISFCService sfcService;
    @Autowired
    SFCReportMapper sfcReportMapper;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ISiteAdviceService siteAdviceService;

    /**
     * 查找SFC报表数据
     * @param jsonMap
     *
     * @return
     * */
    @Override
    public List< Map<String,Object> > searchSFCReport(Map<String, Object> jsonMap) {
        String site = (String)jsonMap.get("site");
        siteAdviceService.existSite( site );
        String procedure = (String)jsonMap.get("procedure");
        procedure = BOHandleUtil.buildFieldGroupHandle( site , procedure );
        String sfc = (String)jsonMap.get("sfc");
        String itemBo = (String)jsonMap.get("itemBo");

        Date startDate = null;
        Date endDate = null ;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (  !StringUtils.isBlank( (String) jsonMap.get( "startDate" ) ) ){
            try {
                startDate = sdf.parse( (String) jsonMap.get( "startDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if ( !StringUtils.isBlank( (String) jsonMap.get( "endDate" ) ) ){
            try {
                endDate = sdf.parse( (String) jsonMap.get( "endDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List< SFC > sfcList = new ArrayList<>();
        List< Map<String,Object> > mapList =  new ArrayList<>();
        if ( StringUtils.isBlank( sfc ) ){
          sfcList = sfcReportMapper.selectSFCListByReport( site , procedure , itemBo ,startDate ,endDate );

          if ( !sfcList.isEmpty() ){

              for ( SFC sfc1 : sfcList ){
                  Map<String,Object> sfcMap = new HashMap<>();
                  Map<String,Object> itemMap=itemAdviceService.existItem( sfc1.getItemBo() );

                  Long total = null;
                  total= sfcReportMapper.selectGRokqtyBysfc( site,sfc1.getSfc() );

                  total = total == null ? 0L : total;
                  Long differ= sfc1.getSfcQty() - total;
                  sfcMap.put("handle",sfc1.getHandle());
                  sfcMap.put("sfc",sfc1.getSfc());
                  sfcMap.put("machineType",sfc1.getMachineType());
                  change( sfc1 , sfcMap );
                  sfcMap.put("itemBo",sfc1.getItemBo());
                  sfcMap.put("description",itemMap.get("DESCRIPTION") );
                  sfcMap.put("planQty",sfc1.getPlanQty());
                  sfcMap.put("sfcQty",sfc1.getSfcQty());
                  sfcMap.put("total",total);
                  sfcMap.put("differ",differ);
                  sfcMap.put("planStartTime",sfc1.getPlanStartTime());
                  sfcMap.put("createdDateTime",sfc1.getCreatedDateTime());
                  sfcMap.put("createUserBo",sfc1.getCreateUserBo().split(",")[1]);

                  mapList.add( sfcMap );
              }
          }

        }else {
           String handle = BOHandleUtil.buildSFCHandle( site , sfc );
           SFC sfc_ = sfcService.selectSFCById( handle );

           Map<String,Object> ITEMMap=itemAdviceService.existItem( sfc_.getItemBo() );
           Map<String,Object> SFCMap = new HashMap<>();

            Long total = null;
            total= sfcReportMapper.selectGRokqtyBysfc( site,sfc_.getSfc() );
            total = total == null ? 0L : total;
            Long differ = sfc_.getSfcQty() - total;
            //total = sfc_.getOkQty();
            SFCMap.put( "handle",sfc_.getHandle() );
            SFCMap.put("sfc",sfc_.getSfc());
            SFCMap.put("machineType",sfc_.getMachineType());
            change( sfc_ , SFCMap );
            SFCMap.put("itemBo",sfc_.getItemBo());
            SFCMap.put("description",ITEMMap.get("DESCRIPTION"));
            SFCMap.put("planQty",sfc_.getPlanQty());
            SFCMap.put("total",total);
            SFCMap.put("differ",differ);
            SFCMap.put("sfcQty",sfc_.getSfcQty());
            SFCMap.put("planStartTime",sfc_.getPlanStartTime());
            SFCMap.put("createdDateTime",sfc_.getCreatedDateTime());
            SFCMap.put("createUserBo",sfc_.getCreateUserBo().split(",")[1]);

           mapList.add( SFCMap );
        }

        return mapList;
    }


    public void  change(SFC sfc ,Map<String,Object> map){
        String procedure = null;
        String status = null;
        procedure = sfc.getProcedureBo().split(",")[1];
        status = sfc.getStatusBo().split(",")[1];

        if ( map == null ){

            if ( ProcedureEnum.GRIND.name().equals( procedure ) ){
               sfc.setProcedureBo( "研磨" );
            }
            if ( ProcedureEnum.CNC.name().equals( procedure ) ){
                sfc.setProcedureBo( "CNC" );
            }

            if ( status.equals("401") ){
                sfc.setStatusBo( "新建" );
            }
            if ( status.equals("403") ){
                sfc.setStatusBo( "进行中" );
            }
            if ( status.equals("405") ){
                sfc.setStatusBo( "已完结" );
            }
            if ( status.equals("409") ){
               sfc.setStatusBo( "已删除" );
            }

        }else {
            if ( ProcedureEnum.GRIND.name().equals( procedure ) ){
                map.put("procedure","研磨");
            }
            if ( ProcedureEnum.CNC.name().equals( procedure ) ){
                map.put("procedure","CNC");
            }

            if ( status.equals("401") ){
                map.put("status","新建");
            }
            if ( status.equals("403") ){
                map.put("status","进行中");
            }
            if ( status.equals("405") ){
                map.put("status","已完结");
            }
            if ( status.equals("409") ){
                map.put("status","已删除");
            }

        }
    }
}
